package cn.gene.timeyan.ums.webapi.ums.service.impl;

import cn.gene.timeyan.common.exception.TimeyanServiceException;
import cn.gene.timeyan.common.restful.JsonResult;
import cn.gene.timeyan.common.restful.ResponseCode;
import cn.gene.timeyan.pojo.ums.model.Attention;
import cn.gene.timeyan.pojo.ums.model.Befollow;
import cn.gene.timeyan.pojo.ums.vo.FollowVO;
import cn.gene.timeyan.pojo.ums.vo.UserSimpleVO;
import cn.gene.timeyan.ums.service.IBefollowService;
import cn.gene.timeyan.ums.service.IUserDetailService;
import cn.gene.timeyan.ums.webapi.ums.mapper.BefollowMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户被关注信息*
 *
 * @author LWG
 * @date 2022/8/3 14:01
 * @since 1.0.0 @gene.cn
 */
@Service
public class BefollowServiceImpl implements IBefollowService {
    @Autowired
    private BefollowMapper befollowMapper;
    @Autowired
    private IUserDetailService userDetailService;
    /**
     * 获取用户的受关注列表
     * @param userId
     * @return
     */
    @Override
    public JsonResult<List<FollowVO>> followList(Long userId) {
        if (userId==null){
            throw new TimeyanServiceException(ResponseCode.ERR_NOTIME,"系统繁忙");
        }
        //获取关注用户的对象的id和关注状态
        List<FollowVO> list=befollowMapper.getFollowListByUserId(userId);
        if (list==null||list.size()==0){
            throw new TimeyanServiceException(ResponseCode.ERR_SELECT,"找不到受关注信息");
        }
        List<FollowVO> removeList=new ArrayList<>();
        //获取用户头像，昵称，简介
        for (FollowVO item:list) {
            System.out.println("item:"+item);
            UserSimpleVO user=userDetailService.selectUserCommentVOByUserId(item.getUserId());
            if (user==null){
                removeList.add(item);
                continue;
            }
            System.out.println("user:"+user);
            BeanUtils.copyProperties(user,item);
            System.out.println("item:"+item);
        }
        list.removeAll(removeList);
        return JsonResult.ok(list);
    }
    @Override
    public void addBefollow(Long userId, Long followUserId) {
        Befollow befollow=new Befollow();
        befollow.setUserId(userId);
        befollow.setFollowUserId(followUserId);
        Attention attention=befollowMapper.selectExists(userId, followUserId);
        if (attention!=null) {
            befollow.setFollowState("1");
        } else {
            befollow.setFollowState("0");
        }
        befollowMapper.insertFollowUser(befollow);
        befollowMapper.addFollersCount(userId);
    }


    @Override
    public void deleteBefollow(Long userId, Long followUserId) {
        String state =befollowMapper.selectFollowState(userId, followUserId);
        if (state.equals("1")) {
            befollowMapper.updateFollowState(userId, followUserId);
        }
        befollowMapper.deleteFollowUserId(userId, followUserId);
        befollowMapper.reduceFollersCount(userId);
    }

}

